#ifndef THINK_THINK_MODEL_RELATION_HASMANYTHROUGH_H
#define THINK_THINK_MODEL_RELATION_HASMANYTHROUGH_H

static char* think_model_relation_hasmanythrough =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Exception;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"use think\\model\\Relation;\n"
	"\n"
	"class HasManyThrough extends Relation\n"
	"{\n"
	"    // 中间关联表外键\n"
	"    protected $throughKey;\n"
	"    // 中间表模型\n"
	"    protected $through;\n"
	"\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @access   public\n"
	"     * @param Model  $parent     上级模型对象\n"
	"     * @param string $model      模型名\n"
	"     * @param string $through    中间模型名\n"
	"     * @param string $foreignKey 关联外键\n"
	"     * @param string $throughKey 关联外键\n"
	"     * @param string $localKey   关联主键\n"
	"     */\n"
	"    public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey)\n"
	"    {\n"
	"        $this->parent     = $parent;\n"
	"        $this->model      = $model;\n"
	"        $this->through    = $through;\n"
	"        $this->foreignKey = $foreignKey;\n"
	"        $this->throughKey = $throughKey;\n"
	"        $this->localKey   = $localKey;\n"
	"        $this->query      = (new $model)->db();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 延迟获取关联数据\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包查询条件\n"
	"     * @return false|\\PDOStatement|string|\\think\\Collection\n"
	"     */\n"
	"    public function getRelation($subRelation = \'\', $closure = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this->query]);\n"
	"        }\n"
	"\n"
	"        return $this->relation($subRelation)->select();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param string  $operator 比较操作符\n"
	"     * @param integer $count    个数\n"
	"     * @param string  $id       关联表的统计字段\n"
	"     * @param string  $joinType JOIN类型\n"
	"     * @return Query\n"
	"     */\n"
	"    public function has($operator = \'>=\', $count = 1, $id = \'*\', $joinType = \'INNER\')\n"
	"    {\n"
	"        return $this->parent;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param  mixed  $where 查询条件（数组或者闭包）\n"
	"     * @param  mixed  $fields   字段\n"
	"     * @return Query\n"
	"     */\n"
	"    public function hasWhere($where = [], $fields = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: hasWhere\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询\n"
	"     * @access public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {}\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询 返回模型对象\n"
	"     * @access public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResult(&$result, $relation, $subRelation, $closure)\n"
	"    {}\n"
	"\n"
	"    /**\n"
	"     * 关联统计\n"
	"     * @access public\n"
	"     * @param Model    $result  数据对象\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @return integer\n"
	"     */\n"
	"    public function relationCount($result, $closure)\n"
	"    {}\n"
	"\n"
	"    /**\n"
	"     * 创建关联统计子查询\n"
	"     * @access public\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @param string   $name    统计数据别名\n"
	"     * @return string\n"
	"     */\n"
	"    public function getRelationCountQuery($closure, &$name = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: withCount\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（进执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {\n"
	"        if (empty($this->baseQuery) && $this->parent->getData()) {\n"
	"            $through      = $this->through;\n"
	"            $alias        = Loader::parseName(basename(str_replace(\'\\\\\', \'/\', $this->model)));\n"
	"            $throughTable = $through::getTable();\n"
	"            $pk           = (new $through)->getPk();\n"
	"            $throughKey   = $this->throughKey;\n"
	"            $modelTable   = $this->parent->getTable();\n"
	"            $this->query->field($alias . \'.*\')->alias($alias)\n"
	"                ->join($throughTable, $throughTable . \'.\' . $pk . \'=\' . $alias . \'.\' . $throughKey)\n"
	"                ->join($modelTable, $modelTable . \'.\' . $this->localKey . \'=\' . $throughTable . \'.\' . $this->foreignKey)\n"
	"                ->where($throughTable . \'.\' . $this->foreignKey, $this->parent->{$this->localKey});\n"
	"            $this->baseQuery = true;\n"
	"        }\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif